package com.example.wujinapi.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig  implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
                .maxAge(3600);
    }


    //addResourceHandlers：添加静态资源处理器
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //  /upload/**   为虚拟地址
        //file:///为真实的本地地址
        //"file:///"System.getProperty("user.dir")+"/src/main/resources/static/upload/img/"

        //映射虚拟路径，解决图片上传回本地服务器后，无法立即回显问题(需要重启程序才能显示上一次上传的图片)
        registry.addResourceHandler("/images/**").addResourceLocations("file:src/main/resources/static/images/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
        /*registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/element-ui/**").addResourceLocations("/element-ui/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");*/
    }

}
